'use strict';

const await = require('await-stream-ready/lib/await');

const Service = require('egg').Service;

class ArticleService extends Service {
    async index(obj) {
        let whereObj = {}
        if(obj.title && obj.title != '' && obj.title != null && obj.title != undefined) {
            whereObj.title = obj.title;
        }
        if(obj.author && obj.author != '' && obj.author != null && obj.author != undefined) {
            whereObj.author = obj.author;
        }
        const result = await this.app.mysql.select('article', {
            where: whereObj,
            orders: [
                ['create_time', 'desc'],
                ['id', 'desc']
            ],
            limit: obj.pageSize || 10,
            offset: (obj.currentPage-1)*obj.pageSize || 0,
        })
        return result
    }
    async add(obj) {
        const result = await this.app.mysql.insert('article', {
            title: obj.title,
            content: obj.content,
            content_html: obj.content_html,
            create_time: new Date().getTime(),
            hits: obj.hits || 0,
            update_time: new Date().getTime(),
            status: 0,
            author: obj.author,
            cover: obj.cover || '',
            cid: obj.cid,
        });
        //console.log(result)
        if (result.affectedRows === 1) {
            return {
                code: 1,
            }
        } else {
            return {
                code: 0,
                message: '上传失败'
            }
        }

    }
}

module.exports = ArticleService;